package com.transsion.clouddemo.core.know

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.mukesh.MarkdownView
import com.transsion.clouddemo.R
import com.transsion.clouddemo.core.DataGenerator
import java.io.BufferedReader
import java.io.InputStreamReader

class GuideFragment : Fragment() {


    companion object {
        fun newInstance(): GuideFragment {
            return GuideFragment()
        }
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val inflateView = inflater.inflate(R.layout.cloud_guide, container, false)


        Log.e("zhangxin","GuideFragment")
        val markdownView: MarkdownView = inflateView.findViewById(R.id.markdown_view)
        // 加载本地 Markdown 文件内容
        val markdownContent = loadMarkdownFromAssets("cloud_Interface.md")

        // 自定义 CSS 样式，确保代码块等比例缩小


        // 将 CSS 样式和 Markdown 内容拼接
        val fullContent = "${DataGenerator.customCss}\n$markdownContent"

        markdownView.setMarkDownText(fullContent)
        return inflateView


    }


    /**
     * 从 assets 目录加载 Markdown 文件内容
     */
    private fun loadMarkdownFromAssets(fileName: String): String {
        val stringBuilder = StringBuilder()
        val assetManager = requireContext().assets
        assetManager.open(fileName).use { inputStream ->
            BufferedReader(InputStreamReader(inputStream)).use { reader ->
                var line = reader.readLine()
                while (line != null) {
                    stringBuilder.append(line).append("\n")
                    line = reader.readLine()
                }
            }
        }
        return stringBuilder.toString()
    }

}
//        val webView = inflateView.findViewById<WebView>(R.id.webView)
//        val progressBar=inflateView.findViewById<ProgressBar>(R.id.progressBar)
//
//        webView.settings.javaScriptEnabled=true
//        webView.webViewClient = WebViewClient()
//        webView.loadUrl(DataGenerator.CLOUD_SDK_DOC)
//
//        // 设置 WebViewClient 来管理基本的页面加载事件
//        webView.webViewClient = object : WebViewClient() {
//            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
//                // 页面开始加载时显示进度条
//                progressBar.visibility = View.VISIBLE
//            }
//
//            override fun onPageFinished(view: WebView?, url: String?) {
//                // 页面加载完成后隐藏进度条
//                progressBar.visibility = View.GONE
//            }
//
//            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
//                // 如果发生错误，也隐藏进度条
//                progressBar.visibility = View.GONE
//            }
//        }
//
//// 设置 WebChromeClient 来管理加载进度
//        webView.webChromeClient = object : WebChromeClient() {
//            override fun onProgressChanged(view: WebView?, newProgress: Int) {
//                // 更新进度条的进度
//                progressBar.progress = newProgress
//                if (newProgress == 100) {
//                    // 页面加载完成后隐藏进度条
//                    progressBar.visibility = View.GONE
//                }
//            }
//        }